//
//  LEEBackgroundView.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/20.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit
import pop

enum clickBottonType: Int {
    case clickBottonSend = 1000
    case clickBottonFriend
    case clickBottonCamera
    case clickBottonLbs
    case clickBottonMusic
    case clickBottonMore
    
}

class LEEBackgroundView: UIView {
    
    var arrayButtons = [LEEButton]()
    var rootVc : UIViewController?
    
    
    //提供给外界接口
    func show(fromVc: UIViewController?) {
        
        rootVc = fromVc
        
        fromVc?.view.addSubview(self)
     
        setBottonAnimation()
    }
    

    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        setEffectImage()
        setupBotton()
    
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        setBottonAnimation(isDismiss: true)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            
            self.removeFromSuperview()
        }
    }
    
    fileprivate lazy var backView = UIImageView(image: UIImage.snapShotCurrentWindow().applyDarkEffect()!)
    
    fileprivate lazy var topImage = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
    
    fileprivate lazy var imgNames = ["tabbar_compose_idea",
                                     "tabbar_compose_friend",
                                     "tabbar_compose_camera",
                                     "tabbar_compose_lbs",
                                     "tabbar_compose_music",
                                     "tabbar_compose_more"]
    fileprivate lazy var imgTitles = ["发布",
                                  "朋友",
                                  "相册",
                                  "位置",
                                  "音乐",
                                  "更多"]

}


extension LEEBackgroundView {
    
    //按钮点击事件
    @objc private func clickBtn(btn: LEEButton) {
        //通过原始值获取枚举对象  不能够判断可选类型的枚举值
        let type = clickBottonType.init(rawValue: btn.tag)!
        switch type {
        case .clickBottonSend:
            clickBtnModal(btn: btn)
        case .clickBottonFriend:
            clickBtnModal(btn: btn)
        case .clickBottonCamera:
            clickBtnModal(btn: btn)
        case .clickBottonLbs:
            clickBtnModal(btn: btn)
        case .clickBottonMusic:
            clickBtnModal(btn: btn)
        case .clickBottonMore:
            clickBtnModal(btn: btn)
        }
    }
    
    private func clickBtnModal(btn: LEEButton) {
        
        UIView.animate(withDuration: 0.5, animations: {
            //当前botton变大
            btn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
            btn.alpha = 0.7
            //其他btn变小
            for otherBtn in self.arrayButtons {
            
                if otherBtn != btn {
                
                 otherBtn.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                    
                 otherBtn.alpha = 0.1
                }
            }
            
        }) { (_) in
            //modal控制器
            //命名空间前缀 （SinaWeibo）
            let prefix = Bundle.main.infoDictionary?["CFBundleName"] as! String
            let VcStr = prefix + "." + "LEESendViewController"
            let clz = NSClassFromString(VcStr) as! UIViewController.Type
            let vc = clz.init()
            let nav = UINavigationController(rootViewController: vc)
            
            self.rootVc?.present(nav, animated: true, completion: {
                
                self.removeFromSuperview()
            })
            
        }
    }

    //添加被处理毛玻璃效果的背景视图
    fileprivate func setEffectImage() {
        //1.添加被处理毛玻璃效果的背景视图  截取屏幕
        //        addSubview(backView)
        
        // 2.用toolBar 高斯模糊（牛！！！！）
        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
        toolBar.barStyle = .default
        self.addSubview(toolBar)
        self.addSubview(topImage)
        
        topImage.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
    }
    
    fileprivate func setupBotton() {
        
        let margin = (kUISCRRENW - 3 * btnImageWidth) / 4
        
        for i in 0..<6 {
            let row = i / 3
            let col = i % 3
            
            let btn = LEEButton(title: imgTitles[i], ImageName: imgNames[i], titleColor: UIColor.darkGray)
            
            btn.tag = 1000 + i
            
            let X = margin + (margin + btnImageWidth) * CGFloat(col)
            let Y = (margin + btnHight) * CGFloat(row) + kUISCRRENH
            
            btn.frame = CGRect(x: X, y: Y, width: btnImageWidth, height: btnHight)
            
            self.addSubview(btn)
            arrayButtons.append(btn)
            
            btn.addTarget(self, action: #selector(clickBtn(btn:)), for: .touchUpInside)
        }
        
    }
    
    fileprivate func setBottonAnimation(isDismiss: Bool = false) {
        
        /*1
         POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
         anSpring.toValue = @(self.square.center.y+300);
         anSpring.beginTime = CACurrentMediaTime() + 1.0f;
         anSpring.springBounciness = 10.0f;
         [self.square pop_addAnimation:anSpring forKey:@"position"];
         */
        
        if isDismiss {
            for (index , btn) in arrayButtons.reversed().enumerated() {
                
                setPopAnimation(btn: btn, index: index, isDismiss: true)
            }
            
        }else{
            
            for (index , btn) in arrayButtons.enumerated() {
                
                setPopAnimation(btn: btn, index: index,isDismiss: false)
            }
            
        }
        
    }
    
    fileprivate func setPopAnimation(btn: LEEButton, index: Int, isDismiss: Bool) {
        
        let anSpring = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
        if isDismiss {
            //加多一点，防止pop动画弹出底面一点点
            anSpring?.toValue = btn.center.y + 400
        }else{
            
            anSpring?.toValue = btn.center.y - 350
        }
        anSpring?.beginTime = CACurrentMediaTime() + Double(index) * 0.05
        anSpring?.springBounciness = 10.0
        anSpring?.springSpeed = 10.0
        
        btn.pop_add(anSpring, forKey: nil)
        
    }

}







